<?php

/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2016/5/2
 * Time: 19:31
 */
class Comment_Model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

//    查询用户列表


    public function get_comments($data){
        $sql="select * from comment where is_deleted='N'";
        if(isset($data['q_id']) && !empty($data['q_id'])){
            $sql=$sql." and q_id='".$data['q_id']."'";
        }
        if(isset($data['comment']) && !empty($data['comment'])){
            $sql=$sql." and comment='".$data['comment']."'";
        }
        if(isset($data['commentor']) && !empty($data['commentor'])){
            $sql=$sql." and commentor='".$data['commentor']."'";
        }
        if(isset($data['sort'])){
            $sql=$sql." order by ".$data['sort'];
        }
        if(isset($data['start_row'])){
            $sql=$sql." limit ".$data['start_row'].",".$data['number'];
        }
        $query=$this->db->query($sql);
        return $query->result_array();
    }

//    根据id查询问题
    public function get_comment_by_id($id){
        $query = $this->db->get_where('comment', array('id' => $id));
        $qr=$query->result_array();
        if($qr){
            return $qr[0];
        }else{
            return null;
        }
    }

//    新增用户
    public function add_comment($data){
        $query=$this->db->insert('comment',$data);
        $affectioned=$this->db->affected_rows();
        if($affectioned==1){
            return $this->db->insert_id();;
        }else{
            return false;
        }
    }

//    删除问题
    public function delete_comment_by_id($id){
        $this->db->set('is_deleted', 'Y');
        $this->db->set('gmt_modify', date("Y-m-d H:i:s" ,time()));
        $this->db->where('id', $id);
        $this->db->update('comment');
        $affectioned=$this->db->affected_rows();
        if($affectioned>0){
            return true;
        }else{
            return false;
        }
    }
}